package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.IncomeDao;
import com.jbp.model.*;
import com.jbp.service.*;
import com.jbp.utils.StrToMillsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 流水分成记录(Income)表服务实现类
 * @author cc
 * @since 2020-07-07 01:18:17
 */
@Service
public class IncomeServiceImpl extends ServiceImpl<IncomeDao, Income> implements IncomeService {
    @Autowired
    private RoomService roomService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private WalletRecordService walletRecordService;
    @Autowired
    private RoomDailyFinanceRecordService roomDailyFinanceRecordService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Income> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null){
            queryWrapper.like("id",params.get("id"));
        }
        IPage<Income> page = this.page(
                new Query<Income>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 流水发放
     * @param totalGiftCoin 消费金额  心钻
     * */
    @Override
    @Transactional
    public void deployIncome(Integer roomId, int totalGiftCoin, String from) {
        Room room = roomService.getById(roomId);
        double aixin = 0d;
        if (room.getAutoIncome()==1) {
            aixin = totalGiftCoin * room.getIncomeRate().doubleValue() / 100;
            //增加流水记录
            Income income = new Income();
            income.setType("auto");
            income.setAixin(BigDecimal.valueOf(aixin));
            income.setRoomId(room.getId());
            income.setUserId(room.getUserId());
            income.setFrm(from);
            save(income);

            Wallet wallet = walletService.getByUserId(room.getUserId());
            BigDecimal after = wallet.getAixin().add(BigDecimal.valueOf(aixin));

            //增加余额变动记录
            WalletRecord walletRecord = new WalletRecord();
            walletRecord.setUserId(room.getUserId());
            walletRecord.setOperationType("lsfc");
            walletRecord.setAddType("add");
            walletRecord.setCoinType("aixin");
            walletRecord.setBeforeBalance(wallet.getAixin());
            walletRecord.setChangeBalance(BigDecimal.valueOf(aixin));
            walletRecord.setAfterBalance(after);
            walletRecordService.save(walletRecord);

            //修改钱包余额
            wallet.setAixin(after);
            walletService.updateWithLock(wallet);
        }

        //每日流水统计
        String date = StrToMillsUtil.millsToStrDate(new Date(), null);
        RoomDailyFinanceRecord roomDailyFinanceRecord = roomDailyFinanceRecordService.getOne(new QueryWrapper<RoomDailyFinanceRecord>()
                .eq("room_id", roomId)
                .eq("frm", from)
                .eq("sta_date", date)
        );
        if (roomDailyFinanceRecord==null) {
            roomDailyFinanceRecord = new RoomDailyFinanceRecord();
            roomDailyFinanceRecord.setRoomId(roomId);
            roomDailyFinanceRecord.setFrm(from);
            roomDailyFinanceRecord.setStaDate(new Date());
            roomDailyFinanceRecord.setLiushui(totalGiftCoin);
            roomDailyFinanceRecord.setFencheng(BigDecimal.valueOf(aixin));
            roomDailyFinanceRecordService.save(roomDailyFinanceRecord);
        } else {
            roomDailyFinanceRecord.setLiushui(totalGiftCoin+roomDailyFinanceRecord.getLiushui());
            roomDailyFinanceRecord.setFencheng(roomDailyFinanceRecord.getFencheng().add(BigDecimal.valueOf(aixin)));
            roomDailyFinanceRecordService.updateById(roomDailyFinanceRecord);
        }
    }
}